<?php
namespace Api\Controller;
use Think\Controller;
class OrderController extends CommonController {
	public function _initialize(){
		
	}
	
	//订单管理显示接口
    public function ordercheck(){
		$id = I('get.id');
		
		$Pay = M('Ware_pay');
		$Shopcar = M('Ware_shopcar');
		
		$list = $Pay->order("addtime desc")->where("user_id={$id}")->select();
		$data = array();
		if( $list == false ){
			$this->response( false , 1 , '没有数据' , true);
		}
		else{
			foreach( $list as $vo){
				$order_id = $vo['order_id'];
				$replylist = array();
				$res = $Shopcar->where("order_id={$order_id}")->select();
				foreach( $res as $vo1){
					$replylist[] = array(
						'head_img'	=>	$vo1['head_img'],
						'ware_name'	=>	$vo1['ware_name'],
						'ware_cart'	=>	$vo1['ware_cart'],
						'cart_list'	=>	$vo1['cart_list'],
						'cart_num'	=>	$vo1['cart_num'],
						'price'		=>	$vo1['price'],
					);
				}
				$vo['reply'] = $replylist;
				$data[] = $vo;
			}
			$this->response( true , 0 , $data , true);
		}
	}
	
	//卖家点击发货接口
	public function orderchange(){
		$order_id = I('get.order_id');
		
		$Pay = M('Ware_pay');
		
		$res = $Pay->where("order_id={$order_id}")->find();
		$data = array();
		if( $res == false){
			$this->response( false , 1 , '没有数据' , true);
		}
		$data = array(
			'status'	=>	3
		);
		$res = $Pay->where("order_id={$order_id}")->save($data);
		$this->response( true , 0 , '发货成功' , true);
	}
	
	//确认收货接口
	public function warereal(){
		$order_id = I('post.order_id');
		
		$Pay = M('Ware_pay');
		
		$res = $Pay->where("order_id={$order_id}")->find();
		$data = array();
		if( $res == false){
			$this->response( false , 1 , '没有数据' , true);
		}
		$data = array(
			'status'	=>	4
		);
		$res = $Pay->where("order_id={$order_id}")->save($data);
		$this->response( true , 0 , '确认收货成功' , true);
	}
	
	//删除订单接口
	public function delware(){
		$order_id = I('post.order_id');
		
		$Pay = M('Ware_pay');
		
		$res = $Pay->where("order_id={$order_id}")->delete();
		
		if( $res == false){
			$this->response( false , 1 , '没有数据' , true);
		}

		$this->response( true , 0 , '删除成功' , true);
	}
	
	//订单详情接口
	public function orderinfo(){
		$order_id	= I('get.order_id');
		$user_id	= $_SESSION['uid'];
		
		$Pay		= M('Ware_pay');
		$Address 	= M('Address');
		$Shopcar 	= M('Ware_shopcar');
		
		$list = $Pay->where("order_id={$order_id}")->find();
		$data = array();
		if( $list == false ){
			$this->response( false , 1 , '没有数据' , true);
		}
		else{
			//获取地址信息
			$ret = $Address->where("click=1")->find();
			//获取订单信息
			$replylist = array();
			$res = $Shopcar->where("order_id={$order_id}")->select();
			foreach( $res as $vo1){
				$replylist[] = array(
					'head_img'	=>	$vo1['head_img'],
					'ware_name'	=>	$vo1['ware_name'],
					'ware_cart'	=>	$vo1['ware_cart'],
					'cart_list'	=>	$vo1['cart_list'],
					'cart_num'	=>	$vo1['cart_num'],
					'price'		=>	$vo1['price'],
				);
			}
			$list['reply']		= $replylist;
			$list['address']	= $ret;
			$data = $list;
		}
		$this->response( true , 0 , $data , true);
	}
}
